<?php

namespace Registro\Frontend\Forms;

use Phalcon\Forms\Form;
use Phalcon\Forms\Element\Text;
use Phalcon\Forms\Element\Password;
use Phalcon\Forms\Element\Submit;
use Phalcon\Forms\Element\Check;
use Phalcon\Forms\Element\Hidden;
use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Validation\Validator\Email;
use Phalcon\Validation\Validator\Identical;

class LoginForm extends Form {

    public function initialize()
    {
        // Email
        $username = new Text( 'user_username', array(
            'placeholder' => 'Email'
        ) );
        $username->setLabel('Username/Email')
            ->addValidators( array(
            new PresenceOf( array(
                'message' => 'The e-mail is required'
                    ) ),
            new Email( array(
                'message' => 'The e-mail is not valid'
                    ) )
        ) );

        $this->add( $username );

        // Password
        $password = new Password( 'user_password', array(
            'placeholder' => 'Password'
        ) );
        $password->setLabel('Password');
        $password->addValidator( new PresenceOf( array(
            'message' => 'The password is required'
        ) ) );

        $this->add( $password );

        // Remember
        $remember = new Check( 'remember', array(
            'value' => 'yes'
        ) );

        $remember->setLabel( 'Remember me' );

        $this->add( $remember );

        // CSRF
        $csrf = new Hidden( 'csrf' );

        $csrf->addValidator( new Identical( array(
            $this->security->checkToken() => 1,
            'message' => 'CSRF validation failed'
        ) ) );


        $this->add( $csrf );

        $submit = new Submit( 'submit', array(
            'value' => 'Login',
            'class' => 'btn btn-success'
        ) );
        
        $this->add( $submit );
    }
    /**
     * Prints messages for a specific element
     */
    public function messages($name)
    {
        if ($this->hasMessagesFor($name)) {
            foreach ($this->getMessagesFor($name) as $message) {
                'Ecc: '.$this->flash->error($message);
            }
        }
    }    
}
